package com.zenithmind.exam.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zenithmind.exam.pojo.entity.QuestionBank;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 题库Mapper接口
 * 
 * @author ZenithMind
 * @since 2024-07-01
 */
@Mapper
public interface QuestionBankMapper extends BaseMapper<QuestionBank> {

    /**
     * 根据分类ID查询题库列表
     * 
     * @param categoryId 分类ID
     * @return 题库列表
     */
    @Select("SELECT * FROM exam_question_bank WHERE category_id = #{categoryId} AND is_deleted = 0 ORDER BY create_time DESC")
    List<QuestionBank> selectByCategoryId(@Param("categoryId") String categoryId);

    /**
     * 根据创建者ID查询题库列表
     * 
     * @param creatorId 创建者ID
     * @return 题库列表
     */
    @Select("SELECT * FROM exam_question_bank WHERE creator_id = #{creatorId} AND is_deleted = 0 ORDER BY create_time DESC")
    List<QuestionBank> selectByCreatorId(@Param("creatorId") String creatorId);

    /**
     * 查询公开题库列表
     * 
     * @return 公开题库列表
     */
    @Select("SELECT * FROM exam_question_bank WHERE is_public = 1 AND status = 1 AND is_deleted = 0 ORDER BY create_time DESC")
    List<QuestionBank> selectPublicBanks();

    /**
     * 分页查询题库列表
     * 
     * @param page 分页参数
     * @param title 题库标题（模糊查询）
     * @param categoryId 分类ID
     * @param creatorId 创建者ID
     * @param status 状态
     * @param isPublic 是否公开
     * @return 分页结果
     */
    IPage<QuestionBank> selectBankPage(Page<QuestionBank> page,
                                      @Param("title") String title,
                                      @Param("categoryId") String categoryId,
                                      @Param("creatorId") String creatorId,
                                      @Param("status") Integer status,
                                      @Param("isPublic") Integer isPublic);

    /**
     * 更新题库题目数量
     * 
     * @param bankId 题库ID
     * @param questionCount 题目数量
     * @return 更新行数
     */
    @Update("UPDATE exam_question_bank SET question_count = #{questionCount}, update_time = NOW() WHERE id = #{bankId}")
    int updateQuestionCount(@Param("bankId") String bankId, @Param("questionCount") Integer questionCount);

    /**
     * 根据标签查询题库列表
     * 
     * @param tag 标签
     * @return 题库列表
     */
    List<QuestionBank> selectByTag(@Param("tag") String tag);

    /**
     * 统计用户创建的题库数量
     * 
     * @param creatorId 创建者ID
     * @return 题库数量
     */
    @Select("SELECT COUNT(*) FROM exam_question_bank WHERE creator_id = #{creatorId} AND is_deleted = 0")
    Integer countByCreatorId(@Param("creatorId") String creatorId);

    /**
     * 查询热门题库列表
     * 
     * @param limit 限制数量
     * @return 热门题库列表
     */
    @Select("SELECT * FROM exam_question_bank WHERE status = 1 AND is_public = 1 AND is_deleted = 0 ORDER BY question_count DESC LIMIT #{limit}")
    List<QuestionBank> selectHotBanks(@Param("limit") Integer limit);
}
